<?php
	//Connect to database
class Meal extends CI_Controller {
	
	public function index(){
		$this->output
		->set_content_type('application/json')
		->set_output(json_encode(array('status' => 0, 'msg'=>'invalid access')));
	}


	//fetchMealCommentByCommentId():
	//INPUT: {POST} commentId
	//OUTPUT: {JSON} an Entry of Meal Data

	public function fetchMealCommentByCommentId(){
		$this->load->database();
		$commentId = $this->input->post('commentId');
/*		$this->db->where('id', $MealID);
		$this->db->where('is_blocked', 0);
		$this->db->from('meal_comment');
		$query = $this->db->get();
*/
$query = $this->db->query('SELECT * FROM meal_comment WHERE id = $commentId AND is_blocked=0');
//echo('SELECT * FROM meal_comment WHERE id = '.$commentId.' AND is_blocked=0');
		if($query->num_row()==1){
			//CASE: comment is existed and not blocked
			$firstRowResult = $query->row(0); //Get the first row of the result.
			$this->output
		    	->set_content_type('application/json')
				->set_output(json_encode(array('status'=>1, 'userID' => $firstRowResult->user, 'meal' => $firstRowResult->meal, 'comment' => $firstRowResult->comment, 'create_date' => $firstRowResult->create_date)));  
		} else {
			//CASE: comment is either blocked or not existed
			$this->output
		    ->set_content_type('application/json')
			->set_output(json_encode(array('status'=>0))); 
		}
	}
	
	//fetchMealCommentByMealId:
	//INPUT: [POST] mealId
	//OUTPUT: {JSON} an Array of Meal Data
	public function fetchMealCommentByMealId(){
		$this->load->database();
		$mealId = $this->input->post('mealId');
		//check for mealID is null
		
		
/*		$this->db->where('id', $MealID);
		$this->db->where('is_blocked', 0);
		$this->db->from('meal_comment');
		$query = $this->db->get();
*/
		$query = $this->db->query('SELECT * FROM meal_comment WHERE meal = '. $mealId.' AND is_blocked=0');

		if($query->num_row()>0){
//			$firstRowResult = $query->row(0); //Get the first row of the result.
			$this->output
		    	->set_content_type('application/json')
				->set_output(json_encode($query->result_array()));
//				->set_output(json_encode(array('status'=>1, 'id'=>$firstRowResult->id, 'userID' => $firstRowResult->user, 'meal' => $firstRowResult->meal, 'comment' => $firstRowResult->comment, 'create_date' => $firstRowResult->create_date)));  
		} else {
			$this->output
		    ->set_content_type('application/json')
			->set_output(json_encode(array('status'=>0)));  
		}
	}
	
	public function fetchMealByMealId(){
		$this->load->database();
		$mealId = $this->input->post('MealId');
		
		
	
	}
	
	
	/*
	$id = $_POST['id'];
	
	//Select non-blocked restaurant comment
    $query01 = "SELECT * FROM meal_comment WHERE id = '$id' AND is_blocked='0'";
	$query01_result = mysql_query($query01);
	if(mysql_num_rows($query01_result) == 1){
		$query01_result_row = mysql_fetch_array($query01_result);
		$user = $query01_result_row['user'];
		$meal = $query01_result_row['meal'];
		$comment = $query01_result_row['comment'];
		$create_date = $query01_result_row['create_date'];
		
		$response["STATUS"] = "1";
		$response["USER"] = $user;
		$response["MEAL"] = $meal;
		$response["COMMENT"] = $comment;
		$response["CREATE_DATE"] = $create_date;
		
		echo json_encode($response);
	}else{
		//Unable to get user
		$response["STATUS"] = "0";
		echo json_encode($response);
	}
	
*/
}
?>
